﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Forum.DataBase
{
    [Serializable]
    public class Data
    {
        public ForumPage forum { get; set; }
        private SortedDictionary<string, UserData> users;

        public Data()
        {
            this.forum = new ForumPage("Main Page");
            users = new SortedDictionary<string, UserData>();
        }

        public ForumPage creatPage(int level,int sublevel, int threadlevel)
        {
            switch (level)
            {
                case 0:
                    return new ForumPage(this.forum);
                case 1:
                    return new ForumPage(this.forum.subs.ElementAt(sublevel).Value);
                case 2:
                    return new ForumPage(this.forum.subs.ElementAt(sublevel).Value.subs.ElementAt(threadlevel).Value);
                default:
                    return null;
            }
        }

        public void initData()
        {
            UserData admin = new UserData("eli", "admin", "cohen", "1234", "");
            admin.permission = Permission.admin;
            this.users.Add("admin",admin);
            ForumPage fp111 = new ForumPage("Meta alecha1");
            ForumPage fp112 = new ForumPage("Meta alecha2");
            ForumPage fp113 = new ForumPage("Meta alecha3");

            ForumPage fp11 = new ForumPage("Eyal golan");
            fp11.subs.Add(1, fp111);
            fp11.subs.Add(2, fp112);
            fp11.subs.Add(3, fp113);

            ForumPage fp12 = new ForumPage("Avi bitter");

            ForumPage fp1 = new ForumPage("Music");
            fp1.subs.Add(1, fp11);

            ForumPage fp2 = new ForumPage("Food");
            ForumPage fp3 = new ForumPage("Movies");

            this.forum.subs.Add(1, fp1);
            this.forum.subs.Add(2, fp2);
            this.forum.subs.Add(3, fp3);
        }
        public bool addUser(string name, string username, string lastname, string password, string email)
        {
            if (!this.users.ContainsKey(username))
            {
                
                this.users.Add(username, new UserData(name,username,lastname,password,email));
                return true;
            }
            return false;
        }

        public bool checkUser(string username, string password)
        {
            try
            {
                return this.users.ContainsKey(username) & this.users[username].password == password;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public void createThread(int subNum,string subject, string body)
        {
            int length = this.forum.subs.ElementAt(subNum).Value.subs.Count;
            ForumPage fp = new ForumPage(subject);
            fp.subs.Add(1, new ForumPage("Subject: " + subject + "\nBody:\n" + body));
            this.forum.subs[subNum+1].subs.Add(length+1, fp);
        }

        public void createSubForum(string subject)
        {
            int length = this.forum.subs.Count;
            ForumPage fp = new ForumPage(subject);
            this.forum.subs.Add(length, fp);
        }

        public void createPost(int subNum,int thrNum, string subject, string body)
        {
            int length = this.forum.subs.ElementAt(subNum).Value.subs.ElementAt(thrNum).Value.subs.Count;
            this.forum.subs[subNum+1].subs[thrNum+1].subs.Add(length+1, new ForumPage("Subject:\n" + subject + "\nBody:\n" + body));
        }

       
        public ForumPage getForum()
        {
            return this.forum;
        }

        public override string ToString()
        {
            return this.forum.ToString();
        }
        public int lengh()
        {
            return this.forum.subs.Count;
        }


        public Permission getPermission(string p)
        {
            return this.users[p].permission;
        }
    }
}
